/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import vn.com.sml.SEFM.BUS.MerchantBUS;
import vn.com.sml.SEFM.BUS.CardBlackBUS;
import vn.com.sml.SEFM.domain.Banks;
import vn.com.sml.SEFM.domain.Cardblacklist;
import vn.com.sml.SEFM.domain.Merchant;

/** 
 *
 * @author Tunp
 */
public class AddCardController extends SimpleFormController {
    private CardBlackBUS cardBUS = new CardBlackBUS();
    private MerchantBUS merchantBUS = new MerchantBUS();

    public void setMerchantBUS(MerchantBUS merchantBUS) {
        this.merchantBUS = merchantBUS;
    }

    public void setCardBUS(CardBlackBUS cardBUS) {
        this.cardBUS = cardBUS;
    }
    
    
    public AddCardController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(Cardblacklist.class);
        setCommandName("card");
//        setSuccessView("detailCard");
        setFormView("addCard");
    }
    
//     @Override
//    protected ModelAndView showForm(HttpServletRequest request, 
//        HttpServletResponse response, BindException errors) throws Exception {
//        
//        ModelAndView mv = super.showForm(request, response, errors);
//        
//        return mv;
//    }
    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    
    @Override
    protected ModelAndView onSubmit(
    HttpServletRequest request, 
    HttpServletResponse response, 
    Object command, 
    BindException errors) throws Exception {
        ModelAndView mv = new ModelAndView("redirectCardBlacklist");
        //Do something...
        
        
        // bat merchantId cua user tai session hien tai
        String merchantId = "1";
        int id = Integer.parseInt(merchantId);
        Merchant mc = merchantBUS.getDvcntById(id);
        
        // nhan doi tuong The blacklist tu giao dien va luu vao co so du lieu
        Cardblacklist card = (Cardblacklist) command;
        card.setMerchant(mc);
        cardBUS.saveCardBlack(card);
        
        // gui doi tuong vua tao len trang success
        mv.addObject("card", card);
        
        return mv;
    }
     
}
